- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathserver.py
44 lines (38 loc) · 1.08 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# gunicorn
# fastapi or flask or aiohttp or sarlette
# Django
# Model -> View -> Template -> Response
defrender_template(template_name='index.html', context={}):
html_str=""
withopen(template_name, 'r') asf:
html_str=f.read()
html_str=html_str.format(**context)
returnhtml_str
defhome(environ):
returnrender_template(
template_name='index.html',
context={}
)
defcontact_us(environ):
returnrender_template(
template_name='contact.html',
context={}
)
defapp(environ, start_response):
path=environ.get("PATH_INFO")
ifpath.endswith("/"):
path=path[:-1]
ifpath=="": # index / root of the web app
data=home(environ)
elifpath=="/contact":
data=contact_us(environ)
else:
data=render_template(template_name='404.html', context={"path": path})
data=data.encode("utf-8")
start_response(
f"200 OK", [
("Content-Type", "text/html"),
("Content-Length", str(len(data)))
]
)
returniter([data])